package de.jmda.home.ui.vaadin.usermgmt;

import javax.ejb.EJB;

import com.vaadin.ui.PasswordField;

import de.jmda.cbo.user.RegistrationRequest;
import de.jmda.cbo.user.User;
import de.jmda.common.ui.web.vaadin.service.DefaultSessionContext;
import de.jmda.home.bl.usermgmt.UserManagementBean;

public class UserChangePasswordComponentController
{
	/**
	 * TODO make use of CDI as soon as possible
	 */
	@EJB
	private UserManagementBean userManagement =
			DefaultSessionContext.get().getUserManagement();

	/**
	 * @param pfPassword
	 * @param pfPasswordNew
	 * @param pfPasswordNewConfirm
	 * 
	 * @throws IllegalStateException if no user is logged in or neither a {@link
	 *         User} nor a {@link RegistrationRequest} can be found
	 */
	public void accept(
			PasswordField pfPassword,
			PasswordField pfPasswordNew,
      PasswordField pfPasswordNewConfirm)
  {
		userManagement.changePassword(
				DefaultSessionContext.get().getUsername(),
				pfPassword.getValue(),
				pfPasswordNew.getValue(),
				pfPasswordNewConfirm.getValue());
  }

	public void cancel()
  {
		DefaultSessionContext.get()
		                     .getEventBus().post(new EventChangePasswordCancel());
  }
}